﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Xml;

namespace BZ.Common
{
    public class DataUtility
    {
        /// <summary>
        /// 向表中追加行
        /// </summary>
        /// <param name="newRow">新行</param>
        /// <param name="newTable">追加表</param>
        /// <param name="templeteTable">模板表，如果为null则会依照列名进行匹配</param>
        /// <returns></returns>
        public static ResultInfo AddDataRow(DataRow newRow, ref DataTable newTable, DataTable templeteTable = null)
        {
            ResultInfo myResult = new ResultInfo(true);

            //判断新加入的行是否包含结构
            if (newRow == null || newRow.Table == null || newRow.Table.Columns.Count<=0)
            {
                myResult.IsSuccess = false;
                myResult.Message = "追加行的结构错误，或者无结构。";
            }

            if (myResult.IsSuccess)
            {
                try
                {
                    if (newTable == null)
                    {
                        //处理新表结构
                        if (templeteTable != null)
                        {
                            newTable = templeteTable.Clone();
                        }
                        else
                        {
                            newTable = new DataTable();
                        }
                    }

                    //如果新表没有结构则开始列结构处理
                    if (newTable.Columns.Count <= 0)
                    {
                        foreach (DataColumn myColumn in newRow.Table.Columns)
                        {
                            newTable.Columns.Add(new DataColumn(myColumn.ColumnName));
                        }
                    }

                    DataColumnCollection myColumns = newTable.Columns;

                    //开始加入数据
                    DataRow myNewRow = newTable.NewRow();

                    foreach (DataColumn myColumn in myColumns)
                    {
                        try
                        {
                            myNewRow[myColumn] = newRow[myColumn.ToString()];
                        }
                        catch
                        {
                            continue;
                        }
                    }

                    newTable.Rows.Add(myNewRow);
                    myResult.IsSuccess = true;
                }
                catch (Exception ex)
                {
                    myResult.IsSuccess = false;
                    myResult.Message = ex.Message;
                }
            }
            return myResult;
        }

        /// <summary>
        /// DataTable转换为xml串
        /// </summary>
        /// <param name="xmlDS">数据表，如果数据表未命名则默认DataTable</param>
        /// <param name="xmlStr">返回序列化的xml串</param>
        /// <returns></returns>
        public static ResultInfo ConvertDataTableToXML(DataTable xmlDS, out string xmlStr)
        {
            MemoryStream stream = null;
            XmlTextWriter writer = null;
            xmlStr = null;
            ResultInfo myResult = new ResultInfo(true);
            try
            {
                stream = new MemoryStream();
                writer = new XmlTextWriter(stream, Encoding.Default);
                if (string.IsNullOrEmpty(xmlDS.TableName))
                {
                    xmlDS.TableName = "DataTable";
                }
                xmlDS.WriteXml(writer);
                int count = (int)stream.Length;
                byte[] arr = new byte[count];
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(arr, 0, count);
                UTF8Encoding utf = new UTF8Encoding();
                xmlStr = utf.GetString(arr).Trim();
            }
            catch(Exception ex)
            {
                xmlStr = String.Empty;
                myResult.IsSuccess = false;
                myResult.Message = ex.Message;
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
            return myResult;
        }

        /// <summary>
        /// xml串转换为dataset
        /// </summary>
        /// <param name="xmlData">xml串</param>
        /// <param name="xmlDs">返回dataset</param>
        /// <returns></returns>
        public static ResultInfo ConvertXMLToDataSet(string xmlData, out DataSet xmlDs)
        {
            StringReader stream = null;
            XmlTextReader reader = null;
            ResultInfo myResult = new ResultInfo(true);
            xmlDs = null;
            try
            {
                DataSet xmlDS = new DataSet();
                stream = new StringReader(xmlData);
                reader = new XmlTextReader(stream);
                xmlDS.ReadXml(reader);
            }
            catch (Exception ex)
            {
                myResult.IsSuccess = false;
                myResult.Message = ex.Message;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return myResult;
        }
    }
}
